﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.KM.Service.Messages;
using ZX.KM.Service.Criterias;
using ZX.KM.Core.Enum;
using ZX.KM.Service.Contracts;
using ZX.Web.UI.Controls;
using ZX.KM.Service.ServiceProxy;
using System.Configuration;
using log4net;
using ZX.WebApp.Extend.AjaxData;

namespace ZX.WebApp.DeskTop.DeskTopItem
{
    public partial class TheNews : ZX.WebApp.KM.KmBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) GetDocumentList();
        }

        /// <summary>
        /// 最新
        /// </summary>
        private void GetDocumentList()
        {
            try
            {
                var htmlTemp = "";
                var DocumentSearchType = Request["DocumentSearchType"];//最新-1  最热-2
                var TypeID = Request["TypeID"];
                var CategaryType = Request["CategaryType"];
                if (DocumentSearchType == null) DocumentSearchType = "thenew";
                if (TypeID == null) TypeID = string.Empty;
                var GTypeID = TypeID == string.Empty ? Guid.Empty : new Guid(TypeID);

                htmlTemp = "<div style='width:100%;height:90%;overflow:auto;font-size: 12px;'>";
                htmlTemp += "   <table cellpadding='0' cellspacing='0' style='width:100%;' border='0'>";

                GetDocumentListRequest gdlr = new GetDocumentListRequest
                {
                    Body = new GetDocumentListCriteria
                    {
                        PageIndex = 1,
                        PageSize = 5,
                        UserID = new Guid(this.UserId.ToString()),
                        CategaryID = GTypeID,
                        CategaryType = (NavigationNodeType)Enum.Parse(typeof(NavigationNodeType), CategaryType),//默认：17，辅助：10
                        SearchType = (DocumentSearchType)Enum.Parse(typeof(DocumentSearchType), DocumentSearchType)
                    }
                };
                gdlr.Header = CurrentRequestHeader;

                IDocumentService target = ServiceProxyFactory.GetService<IDocumentService>();
                GetDocumentListResponse actual = target.GetDocumentList(gdlr);
                List<ZX.KM.Service.DataTransferObjects.DocumentListInfo> list = actual.Body.Result;

                if (list.Count > 0)
                {
                    //最新最热  typeid为0 ，其他传入节点类型
                    //string KMINDEX = ConfigurationManager.AppSettings["KMINDEX"].ToString().ToLower();
                    string KMINDEX = this.GetCurrentHost + "/kM/index.aspx";

                    //htmlTemp += "<tr><th style='border-left: 1px solid #C1DAD7;'>&nbsp;</th></tr>";
                    htmlTemp += "<div style='width:100%;height:1px;'></div>";
                    foreach (ZX.KM.Service.DataTransferObjects.DocumentListInfo model in list)
                    {
                        var uid = this.UserId.Value.ToString();
                        var newObj = "{\"DocumentID\":\"" + model.ID.ToString() + "\",\"SearchType\":1,\"typeid\":\"\",\"leftid\":\"" + model.FolderID.ToString() + "\",\"UserID\":\"" + uid + "\"}";
                        newObj = ZX.KM.Core.Common.SecurityUtility.Encrypt(newObj, web_url_key);
                        newObj = HttpUtility.UrlEncode(newObj);
                        htmlTemp += "<tr style='height:30px;'><td style='border-bottom:1px dashed #ccc;'>";
                        htmlTemp += "&nbsp;<img align='absMiddle' src='../../Images/arrow11.gif' width='9' height='9'>&nbsp;";
                        string strTitle = Staple.PadString(model.Title, 30, true);
                        htmlTemp += "<a href='javascript:void(0);' class='ClassFromCRM' CRMObj=" + newObj + " KmIndex=" + KMINDEX + ">" + strTitle + "";
                        htmlTemp += "【" + model.PublishByName + "】";
                        htmlTemp += "</a></td>";
                        string dtStr = " <font color=\"#999999\">{0}</font>";
                        dtStr = String.Format(dtStr, model.PublishTime.Value.ToString("yyyy-MM-dd HH:mm"));
                        htmlTemp += "<td style='text-align:right;border-bottom:1px dashed #ccc;'>" + dtStr + "&nbsp;</td>";
                        htmlTemp += "</tr>";
                    }
                }
                else
                {
                    htmlTemp += "<tr><th>没有数据...</th></tr>";
                }
                htmlTemp += "</table></div>";

                Response.Clear();
                Response.Write(htmlTemp);
                Response.End();
            }
            catch (Exception ex)
            {
                var logger = LogManager.GetLogger(GetType());
                logger.Error(ex.Message, ex);
            }
        }
    }
}